package com.toilet.easyvideofx.view;

import com.toilet.easyvideofx.constant.StyleConstant;
import com.toilet.easyvideofx.manager.DownloadService;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

/**
 * 下载展示
 */
public class View implements Callback<ListView<DownloadViewModel>, ListCell<DownloadViewModel>> {
    /**
     * 进度条宽度
     */
    private final DoubleBinding progressBarWidth;

    public View(DoubleBinding progressBarWidth) {
        this.progressBarWidth = progressBarWidth;
    }

    @Override
    public ListCell<DownloadViewModel> call(ListView<DownloadViewModel> param) {
        return new ListCell<>() {
            @Override
            protected void updateItem(DownloadViewModel item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    VBox vBox = new VBox();
                    //使用main的css
                    vBox.getStylesheets().add("css/controller/main-controller.css");
                    vBox.getStyleClass().add("list-view-download-vbox");
                    HBox hBox = new HBox();
                    hBox.getStylesheets().add("css/controller/main-controller.css");
                    hBox.getStyleClass().add("list-view-download-vbox-hbox");
                    ImageView view = new ImageView("image/video.ico");
                    view.setPreserveRatio(true);
                    view.setFitHeight(30);
                    Button button = new Button(item.getDownloadName());
                    DownloadService downloadService = new DownloadService(item.getDoubleProperty(), item.getFileId());
                    button.setOnMouseClicked(event -> {
                        boolean running = downloadService.isRunning();
                        System.out.println(item.getDoubleProperty());
//                        downloadService.restart();
                        downloadService.restart();
                        ReadOnlyDoubleProperty readOnlyDoubleProperty = downloadService.progressProperty();
                        item.doublePropertyProperty().bind(readOnlyDoubleProperty);
                    });
                    Button cancel = new Button("取消");
                    cancel.setOnMouseClicked(event -> downloadService.cancel());

                    hBox.getChildren().addAll(view, button, cancel);
                    HBox showProgress = new HBox();
                    hBox.getStylesheets().add("css/controller/main-controller.css");
                    hBox.getStyleClass().add("list-view-download-vbox-hbox");

                    ProgressBar progressBar = new ProgressBar();
                    progressBar.prefWidthProperty().bind(progressBarWidth);
                    progressBar.progressProperty().bind(item.doublePropertyProperty());
                    Label label = new Label("111");
                    label.textProperty().bind(item.doublePropertyProperty().asString());
                    showProgress.getChildren().addAll(progressBar, label);
                    vBox.getChildren().addAll(hBox, showProgress);
                    this.setGraphic(vBox);
                    //设置移入变色
                    this.setOnMouseEntered(event -> this.setStyle(StyleConstant.LIST_VIEW_STYLE_ENTER));
                    this.setOnMouseExited(event -> this.setStyle(StyleConstant.LIST_VIEW_STYLE_EXITED));
                }
            }
        };
    }

    /**
     * 实例化
     *
     * @return View
     */
    public static View newInstance(DoubleBinding progressBarWidth) {
        return new View(progressBarWidth);
    }
}
